﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewAI.Client;
namespace NewAI.FSM.SocketConnectFSM
{
    public class ConnectToGameServerState : State<AIClientSocket>
    {
        public override void Enter(AIClientSocket entity)
        {
            try
            {
                entity.ClientSocket = ServerConnector.Connect(entity.Host, entity.Port);
                entity.ClientSocket.OnSocketException += entity.OnSocketException;
                entity.ClientSocket.OnReceiveData += entity.OnReceiveData;
                entity.ChangeState(new SendMsgToGameServerState(GetRegistrationSendText()));
            }
            catch (Exception)
            {
                if (entity.WillClose == false)
                    entity.ChangeState(new TimeDelayState(3000));
            }
        }
        private string GetRegistrationSendText()
        {
            string json = RegistrationJson.Registration;
            json = json.Replace("【ID】", GV.OurTeam.TeamID.ToString());
            json = json.Replace("【Name】", GV.OurTeam.TeamName);
            return json;
        }

    }
}
